package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.RoleResourcesPermissionDao;
import com.suyang.syt.model.RoleResourcesPermission;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.RoleResourcesPermissionDTO;
import com.suyang.syt.service.interfaces.RoleResourcesPermissionService;

/**
 * @author administrator
 * @date 2016-2-24 15:44:06
 * @description
 */
@Service
public class RoleResourcesPermissionServiceImpl implements RoleResourcesPermissionService {
	@Autowired
	private RoleResourcesPermissionDao roleResourcesPermissionDao;

	@Override
	public PagerEasyUI<RoleResourcesPermissionDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2) throws Exception {
		StringBuffer hql = new StringBuffer(" from RoleResourcesPermission Rn where 1=1 ");
		if (StringUtils.isNotEmpty(createDate1)) {
			hql.append(" and Rn.createTime >= '").append(createDate1).append("' ");
		}
		if (StringUtils.isNotEmpty(createDate2)) {
			hql.append(" and Rn.createTime <= '").append(createDate2).append("' ");
		}
		if (StringUtils.isNotEmpty(sort)) {
			hql.append(" order by Rn.").append(sort);
		}
		if (StringUtils.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<RoleResourcesPermissionDTO> easyUIPager = new PagerEasyUI<RoleResourcesPermissionDTO>();
		List<RoleResourcesPermission> roleResourcesPermissions = roleResourcesPermissionDao.findByEasyUI(hql.toString(), page, rows);
		List<RoleResourcesPermissionDTO> roleResourcesPermissionDTOs = new ArrayList<RoleResourcesPermissionDTO>();
		RoleResourcesPermissionDTO roleResourcesPermissionDTO = null;
		for (RoleResourcesPermission roleResourcesPermission : roleResourcesPermissions) {
			roleResourcesPermissionDTO = new RoleResourcesPermissionDTO(roleResourcesPermission);
			roleResourcesPermissionDTOs.add(roleResourcesPermissionDTO);
		}
		easyUIPager.setRows(roleResourcesPermissionDTOs);
		easyUIPager.setTotal((Long) roleResourcesPermissionDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	private RoleResourcesPermission setRoleResourcesPermission(RoleResourcesPermission roleResourcesPermission, RoleResourcesPermissionDTO roleResourcesPermissionDTO, boolean isAdd) {
		roleResourcesPermission.setId(roleResourcesPermissionDTO.getId());
		roleResourcesPermission.setPermissionIds(roleResourcesPermissionDTO.getPermissionIds());
		roleResourcesPermission.setResourceId(roleResourcesPermissionDTO.getResourceId());
		roleResourcesPermission.setRoleId(roleResourcesPermissionDTO.getRoleId());
		return roleResourcesPermission;
	}

	@Override
	public AjaxObj add(RoleResourcesPermissionDTO roleResourcesPermissionDTO) throws Exception {
		if (roleResourcesPermissionDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		RoleResourcesPermission roleResourcesPermission = new RoleResourcesPermission();
		roleResourcesPermissionDao.add(setRoleResourcesPermission(roleResourcesPermission, roleResourcesPermissionDTO, true));
		return new AjaxObj(1, "增加成功");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (StringUtils.isEmpty(ids)) {
			return new AjaxObj(0, "参数错误");
		}
		roleResourcesPermissionDao.deletes(ids);
		return new AjaxObj(1, "删除成功");
	}

	@Override
	public AjaxObj update(RoleResourcesPermissionDTO roleResourcesPermissionDTO) throws Exception {
		if (roleResourcesPermissionDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		RoleResourcesPermission roleResourcesPermission = roleResourcesPermissionDao.load(roleResourcesPermissionDTO.getId());
		if (roleResourcesPermission == null) {
			return new AjaxObj(0, "数据库异常!");
		}
		roleResourcesPermissionDao.update(setRoleResourcesPermission(roleResourcesPermission, roleResourcesPermissionDTO, false));
		return new AjaxObj(1, "更新成功");
	}

}
